<?php

/**
 * Extends the default Twig security policy - all tags and methods allowed
 * checkMethodAllowed and checkPropertyAllowed fixes some php weirdnesssssses for doctrine models with behaviours
 *
 */
class Twig_Sandbox_SecurityPolicy_Berta extends Twig_Sandbox_SecurityPolicy {
    public function checkSecurity($tags, $filters, $functions){
    	return true;
    }

    public function checkMethodAllowed($obj, $method){
        if ($obj instanceof Twig_TemplateInterface || $obj instanceof Twig_Markup) {
            return true;
        }

    	$method=strtolower($method);

        $allowed = false;
        foreach ($this->allowedMethods as $class => $methods) {
            if ($obj instanceof $class) {
            	if (!is_array($methods)){
            		$methods=array($methods);
            	}

                $allowed = in_array($method, $methods);

                // for getMethodName check permission for MethodName
                if (!$allowed && strpos($method, "get")===0){
                	$allowed = in_array(substr($method, 3), $methods);
                }

                // for methodName check permission for getMethodName
                if (!$allowed && strpos($method, "get")!==0){
                	$allowed = in_array("get{$method}", $methods);
                }

                break;
            }
        }

        if (!$allowed) {
            throw new Twig_Sandbox_SecurityError(sprintf('Calling "%s" method on a "%s" object is not allowed.', $method, get_class($obj)));
        }
    }

    public function checkPropertyAllowed($obj, $property){
        $allowed = false;
        foreach ($this->allowedProperties as $class => $properties) {
            if ($obj instanceof $class) {
            	if ($properties=="*"){
            		return true;
            	}
                $allowed = in_array($property, is_array($properties) ? $properties : array($properties));

                break;
            }
        }

        if (!$allowed) {
            throw new Twig_Sandbox_SecurityError(sprintf('Calling "%s" property on a "%s" object is not allowed.', $property, get_class($obj)));
        }
    }



}